﻿var userApp = angular.module("userInfoApp",[]);


userApp.controller("userInfoCtrl",function ($scope,$http,$rootScope) {

});

userApp.controller("addressCtrl",function ($scope,$http,$rootScope) {
    $scope.ToDefaultAddress = function (index) {
        var addressLength = $scope.addresses.length;
        for(var i = 0; i < addressLength; i++){
            $scope.addresses[i].priority = 0;
            if(i==index){
                $scope.addresses[index].priority = 1;
            }
        }
        var updateAddresPriorityReq = {
            url: "/CHa/user/"+$rootScope.uid,
            method:"PUT",
            data:{address:$scope.addresses},
            headers:{
                "Content-type":"application/json"
            }
        }
        $http(updateAddresPriorityReq).then(function (res) {

        })
    }

    $scope.newAddress = "";
    $scope.addAddress = function () {
        if($scope.newAddress!=""){
            var isExit = $scope.addresses.some(function (o,inx,ary) {
                return angular.equals(o.address,$scope.newAddress);
            });
            if(isExit == false){
                $scope.addresses.push({address:$scope.newAddress,priority:0});
                var addAddressReq = {
                    url: "/CHa/user/"+$rootScope.uid,
                    method:"PUT",
                    data:{address:$scope.addresses},
                    headers:{
                        "Content-type":"application/json"
                    }
                }
                $http(addAddressReq).then(function (res) {

                })
            }else{
                alert("已有相同地址！");
            }
        }else{
            alert("地址不能为空！");
        }
    }
});

userApp.controller('passwordCtrl',function ($scope,$http,$rootScope) {
    $scope.password = "";
    $scope.newPassword = "";
    $scope.updatePassword = function () {
        if($scope.password == ""){
            alert("密码不能为空");
        }else{
            if($scope.password ==  $scope.newPassword ){
                var updatePSReq = {
                    url:"/CHa/user/"+$rootScope.uid,
                    method:"PUT",
                    data:{password:$scope.password},
                    headers:{
                        "Content-type":"application/json"
                    }
                }
                $http(updatePSReq).then(function (res) {
                    alert("修改密码成功");
                    $scope.password = "";
                    $scope.newPassword = "";
                })
            }else{
                alert("两次输入不相同");
            }
        }
    }
});

userApp.controller('userCtrl',function ($scope,$http,$rootScope) {
    var req = {
        method:"GET",
        url:"/CHa/session"
    }
    $http(req)
        .then(function (res) {
            if(res.data.account!=undefined){
                $rootScope.uid = res.data.uid;
                $scope.userAccount = res.data.account;
                $scope.hasLogin = true;


                $scope.isShowAddress = true;
                $scope.isShowInfo = false;

                $scope.showAddress =function () {
                    $scope.isShowAddress = true;
                    $scope.isShowInfo = false;
                }

                $scope.showInfo =function () {
                    $scope.isShowAddress = false;
                    $scope.isShowInfo = true;
                }


                var getUserReq = {
                    url: "/CHa/user/"+$rootScope.uid,
                    method: "GET",
                }
                $http(getUserReq).then(function(res){
                    console.log(res.data);
                    $scope.userInfo = res.data;
                    $scope.userAccount = $scope.userInfo.account;
                    $scope.addresses = $scope.userInfo.address;
                })
            }else{
                $scope.hasLogin = false;
            }


        });

    $scope.logOut = function () {
        var clearSeesionReq = {
            url:"/CHa/session",
            method:"PUT"
        }
        $http(clearSeesionReq).then(function (res) {
            window.location.href = "home.html";
        })

    }
});

